package com.example.badyloans.servlet;

import com.example.badyloans.common.ReturnResult;
import com.example.badyloans.dao.TUserWalletMapper;
import com.example.badyloans.pojo.TUserWallet;
import com.example.badyloans.util.MD5;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.badyloans.dao.TUserAccountMapper;
import com.example.badyloans.pojo.TUserAccount;
import com.example.badyloans.servlet.TUserAccountService;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class TUserAccountServiceImpl implements TUserAccountService{

    @Resource
    private TUserAccountMapper tUserAccountMapper;
    @Resource
    private TUserAccountService tUserAccountService;
    @Resource
    private TUserWalletMapper tUserWalletMapper;


    @Override
    public int deleteByPrimaryKey(String id) {
        return tUserAccountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ReturnResult insertSelective(TUserAccount tUserAccount) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
//            String password1=request.getParameter("confirmPwd");
        if(StringUtils.isEmpty(tUserAccount.getUsername()) || StringUtils.isEmpty(tUserAccount.getPassword())){
            return ReturnResult.succes().fail("用户名或密码不能为空");
        }
        tUserAccount.setPassword(MD5.encode(tUserAccount.getPassword()));
        tUserAccount.setId(uuid);
        tUserAccount.setAccountStatus(1);//状态
        tUserAccount.setAccountType(1);//普通用户
        tUserAccount.setCreateTime(LocalDateTime.now());//创建时间
        tUserAccount.setFillUserinfo(0);//完善个人信息
        int count=tUserAccountMapper.insertSelective(tUserAccount);
        if(count>0){
            TUserWallet tUserWallet=new TUserWallet();
            tUserWallet.setAccountId(uuid);
            tUserWallet.setAvailableAmount(10000*100L);
            int count1=tUserWalletMapper.insertSelective(tUserWallet);
            if(count1>0){
                return ReturnResult.success(null,null);
            }else{
                return ReturnResult.fail("新增失败");
            }
        }else{
            return ReturnResult.fail("新增失败");
        }
    }

    @Override
    public TUserAccount selectByPrimaryKey(String id) {
        return tUserAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TUserAccount record) {
        return tUserAccountMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public boolean countUserAccountUsername(String username) {
        return tUserAccountMapper.countUserAccountByUsername(username)<1;
    }

    @Override
    public TUserAccount countUserAccountdenglu1(String username, String password) {
        return tUserAccountMapper.countUserAccountdenglu(username,password);
    }

}
